package com.zdzx.utils;

import java.io.File;
import java.io.InputStream;

import com.zdzx.model.constant.SystemConfig;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;

public class MinioUtil {
	
	
	
	public static MinioClient minioClient;
	static {
		try {
//			minioClient = new MinioClient(SystemConfig.ossIpDoMain, "szlusername", "szlpassword");
			minioClient = new MinioClient(SystemConfig.ossIpDoMain, "minioadmin", "minioadmin");
			//minioClient = new MinioClient(SystemConfig.ossIpDoMain, "minioadmin", "minioadmin");
			// MinioClient s3Client = new MinioClient("s3.amazonaws.com",
			// "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY"); s3
		} catch (InvalidEndpointException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidPortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 创建目录
	 * @param bucketName   目录名
	 * @return
	 */
	public static int makeBucket(String bucketName) {
		try {
			if (!minioClient.bucketExists(bucketName)) {
				  minioClient.makeBucket(bucketName);
			}else
				return 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		return 1;
	}
	/**
	 * 上传
	 * @param bucketName  目录
	 * @param fileName  文件名
	 * @return   -1 文件存在   null 上传失败
	 */
	public static String uploadFile(String bucketName, String objectName,InputStream input,PutObjectOptions options) {
		makeBucket(bucketName);
		try {
			minioClient.statObject(bucketName, objectName);
			return null;
		} catch (Exception e) {
			
		}
		try {
			//生成256位AES key。
			 // KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
			 // symKeyGenerator.init(256);
			 // SecretKey symKey = symKeyGenerator.generateKey();
			minioClient.putObject(bucketName, objectName, input, options);//options
			return bucketName+"/"+objectName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 上传
	 * @param bucketName  目录
	 * @param fileName  文件名
	 * @return
	 */
	public static String uploadFile(String bucketName, String objectName,String filePath) {
		makeBucket(bucketName);
		try {
			minioClient.statObject(bucketName, objectName);
			return null;
		} catch (Exception e) {
			
		}
		
		try {
			
			//生成256位AES key。
			 // KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
			 // symKeyGenerator.init(256);
			 // SecretKey symKey = symKeyGenerator.generateKey();
			ObjectStat statObject = minioClient.statObject(bucketName, objectName);
			if(statObject!=null) {
				return "-1";
			}
			File file=new File(filePath);
			PutObjectOptions options=new PutObjectOptions(file.length(), -1);
			minioClient.putObject(bucketName, objectName, filePath, options);//options
			return bucketName+"/"+objectName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 下载
	 * @param bucketName  目录
	 * @param fileName  文件名
	 * @return
	 */
	public static InputStream downFile(String bucketName, String objectName) {
		try {
			//生成256位AES key。
			 // KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
			 // symKeyGenerator.init(256);
			 // SecretKey symKey = symKeyGenerator.generateKey();
			//minioClient.statObject(bucketName, objectName);
			InputStream input = minioClient.getObject(bucketName, objectName);
			System.out.println(input.available()+"---");
			return input;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static boolean remove(String bucketName,String objectName){
		try {
		minioClient.removeObject(bucketName, objectName);
		return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 生成临时访问地址
	 * @param bucketName
	 * @param objectName
	 * @return
	 */
	public static String getExpiryUrl(String bucketName,String objectName){
		try {
			
			String url = minioClient.presignedGetObject(bucketName, objectName,60*60); //秒
			//if(!StringUtils.isEmpty(url)) {
			//	url=url.replace(SystemConfig.ossIpDoMain, SystemConfig.ossIp);
			//}
			return url;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
}
